package cn.edu.scau.cmi.hombio.service;

import cn.edu.scau.cmi.hombio.dao.EmployeeDAO;
import cn.edu.scau.cmi.hombio.dao.FeedDAO;
import cn.edu.scau.cmi.hombio.dao.PigstyDAO;
import cn.edu.scau.cmi.hombio.dao.TaskDAO;

import cn.edu.scau.cmi.hombio.domain.Employee;
import cn.edu.scau.cmi.hombio.domain.Feed;
import cn.edu.scau.cmi.hombio.domain.Pigsty;
import cn.edu.scau.cmi.hombio.domain.Task;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Feed entities
 * 
 */

@Service("FeedService")

@Transactional
public class FeedServiceImpl implements FeedService {

	/**
	 * DAO injected by Spring that manages Employee entities
	 * 
	 */
	@Autowired
	private EmployeeDAO employeeDAO;

	/**
	 * DAO injected by Spring that manages Feed entities
	 * 
	 */
	@Autowired
	private FeedDAO feedDAO;

	/**
	 * DAO injected by Spring that manages Pigsty entities
	 * 
	 */
	@Autowired
	private PigstyDAO pigstyDAO;

	/**
	 * DAO injected by Spring that manages Task entities
	 * 
	 */
	@Autowired
	private TaskDAO taskDAO;

	/**
	 * Instantiates a new FeedServiceImpl.
	 *
	 */
	public FeedServiceImpl() {
	}

	/**
	 * Delete an existing Task entity
	 * 
	 */
	@Transactional
	public Feed deleteFeedTask(Integer feed_id, Integer related_task_id) {
		Feed feed = feedDAO.findFeedByPrimaryKey(feed_id, -1, -1);
		Task related_task = taskDAO.findTaskByPrimaryKey(related_task_id, -1, -1);

		feed.setTask(null);
		related_task.getFeeds().remove(feed);
		feed = feedDAO.store(feed);
		feedDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		taskDAO.remove(related_task);
		taskDAO.flush();

		return feed;
	}

	/**
	 * Save an existing Employee entity
	 * 
	 */
	@Transactional
	public Feed saveFeedEmployee(Integer id, Employee related_employee) {
		Feed feed = feedDAO.findFeedByPrimaryKey(id, -1, -1);
		Employee existingemployee = employeeDAO.findEmployeeByPrimaryKey(related_employee.getId());

		// copy into the existing record to preserve existing relationships
		if (existingemployee != null) {
			existingemployee.setId(related_employee.getId());
			existingemployee.setNumber(related_employee.getNumber());
			existingemployee.setName(related_employee.getName());
			existingemployee.setPassword(related_employee.getPassword());
			existingemployee.setLogOn(related_employee.getLogOn());
			existingemployee.setRfid(related_employee.getRfid());
			existingemployee.setSex(related_employee.getSex());
			existingemployee.setBirthday(related_employee.getBirthday());
			existingemployee.setState(related_employee.getState());
			existingemployee.setMemo(related_employee.getMemo());
			related_employee = existingemployee;
		}

		feed.setEmployee(related_employee);
		related_employee.getFeeds().add(feed);
		feed = feedDAO.store(feed);
		feedDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		return feed;
	}

	/**
	 */
	@Transactional
	public Feed findFeedByPrimaryKey(Integer id) {
		return feedDAO.findFeedByPrimaryKey(id);
	}

	/**
	 * Delete an existing Employee entity
	 * 
	 */
	@Transactional
	public Feed deleteFeedEmployee(Integer feed_id, Integer related_employee_id) {
		Feed feed = feedDAO.findFeedByPrimaryKey(feed_id, -1, -1);
		Employee related_employee = employeeDAO.findEmployeeByPrimaryKey(related_employee_id, -1, -1);

		feed.setEmployee(null);
		related_employee.getFeeds().remove(feed);
		feed = feedDAO.store(feed);
		feedDAO.flush();

		related_employee = employeeDAO.store(related_employee);
		employeeDAO.flush();

		employeeDAO.remove(related_employee);
		employeeDAO.flush();

		return feed;
	}

	/**
	 * Load an existing Feed entity
	 * 
	 */
	@Transactional
	public Set<Feed> loadFeeds() {
		return feedDAO.findAllFeeds();
	}

	/**
	 * Return all Feed entity
	 * 
	 */
	@Transactional
	public List<Feed> findAllFeeds(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Feed>(feedDAO.findAllFeeds(startResult, maxRows));
	}

	/**
	 * Save an existing Task entity
	 * 
	 */
	@Transactional
	public Feed saveFeedTask(Integer id, Task related_task) {
		Feed feed = feedDAO.findFeedByPrimaryKey(id, -1, -1);
		Task existingtask = taskDAO.findTaskByPrimaryKey(related_task.getId());

		// copy into the existing record to preserve existing relationships
		if (existingtask != null) {
			existingtask.setId(related_task.getId());
			existingtask.setTaskNumber(related_task.getTaskNumber());
			existingtask.setCreateTime(related_task.getCreateTime());
			existingtask.setStartTime(related_task.getStartTime());
			existingtask.setEndTime(related_task.getEndTime());
			existingtask.setMemo(related_task.getMemo());
			related_task = existingtask;
		}

		feed.setTask(related_task);
		related_task.getFeeds().add(feed);
		feed = feedDAO.store(feed);
		feedDAO.flush();

		related_task = taskDAO.store(related_task);
		taskDAO.flush();

		return feed;
	}

	/**
	 * Return a count of all Feed entity
	 * 
	 */
	@Transactional
	public Integer countFeeds() {
		return ((Long) feedDAO.createQuerySingleResult("select count(o) from Feed o").getSingleResult()).intValue();
	}

	/**
	 * Save an existing Feed entity
	 * 
	 */
	@Transactional
	public void saveFeed(Feed feed) {
		Feed existingFeed = feedDAO.findFeedByPrimaryKey(feed.getId());

		if (existingFeed != null) {
			if (existingFeed != feed) {
				existingFeed.setId(feed.getId());
				existingFeed.setTime(feed.getTime());
				existingFeed.setMemo(feed.getMemo());
			}
			feed = feedDAO.store(existingFeed);
		} else {
			feed = feedDAO.store(feed);
		}
		feedDAO.flush();
	}

	/**
	 * Save an existing Pigsty entity
	 * 
	 */
	@Transactional
	public Feed saveFeedPigsty(Integer id, Pigsty related_pigsty) {
		Feed feed = feedDAO.findFeedByPrimaryKey(id, -1, -1);
		Pigsty existingpigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsty.getId());

		// copy into the existing record to preserve existing relationships
		if (existingpigsty != null) {
			existingpigsty.setId(related_pigsty.getId());
			existingpigsty.setNumber(related_pigsty.getNumber());
			existingpigsty.setRfid(related_pigsty.getRfid());
			existingpigsty.setCapacity(related_pigsty.getCapacity());
			existingpigsty.setMemo(related_pigsty.getMemo());
			related_pigsty = existingpigsty;
		}

		feed.setPigsty(related_pigsty);
		related_pigsty.getFeeds().add(feed);
		feed = feedDAO.store(feed);
		feedDAO.flush();

		related_pigsty = pigstyDAO.store(related_pigsty);
		pigstyDAO.flush();

		return feed;
	}

	/**
	 * Delete an existing Feed entity
	 * 
	 */
	@Transactional
	public void deleteFeed(Feed feed) {
		feedDAO.remove(feed);
		feedDAO.flush();
	}

	/**
	 * Delete an existing Pigsty entity
	 * 
	 */
	@Transactional
	public Feed deleteFeedPigsty(Integer feed_id, Integer related_pigsty_id) {
		Feed feed = feedDAO.findFeedByPrimaryKey(feed_id, -1, -1);
		Pigsty related_pigsty = pigstyDAO.findPigstyByPrimaryKey(related_pigsty_id, -1, -1);

		feed.setPigsty(null);
		related_pigsty.getFeeds().remove(feed);
		feed = feedDAO.store(feed);
		feedDAO.flush();

		related_pigsty = pigstyDAO.store(related_pigsty);
		pigstyDAO.flush();

		pigstyDAO.remove(related_pigsty);
		pigstyDAO.flush();

		return feed;
	}
}
